package com.luck.easyfoodbatch.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.adapter.MenuOrderAdapter;
import com.luck.easyfoodbatch.db.MessageDB;
import com.luck.easyfoodbatch.entity.Message;
import com.luck.easyfoodbatch.listener.OnItemClickListener;
import com.luck.easyfoodbatch.mvp.mine.DelMessageModel;
import com.luck.easyfoodbatch.mvp.mine.DelMessagePresenter;
import com.luck.easyfoodbatch.mvp.mine.DelMessageView;
import com.luck.easyfoodbatch.mvp.model.DelMessage;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Utils;
import com.luck.easyrecyclerview.decoration.RecycleViewDivider;
import com.luck.easyrecyclerview.sideslip.Closeable;
import com.luck.easyrecyclerview.sideslip.OnSwipeMenuItemClickListener;
import com.luck.easyrecyclerview.sideslip.SwipeMenu;
import com.luck.easyrecyclerview.sideslip.SwipeMenuCreator;
import com.luck.easyrecyclerview.sideslip.SwipeMenuItem;
import com.luck.easyrecyclerview.sideslip.SwipeMenuRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrderMessageActivity extends BaseActivity implements DelMessageView {

    @BindView(R.id.recyclerView)
    SwipeMenuRecyclerView mSwipeMenuRecyclerView;
    @BindView(R.id.iv_nodata)
    ImageView iv_nodata;
    private MessageDB messageDB;
    private MenuOrderAdapter adapter;
    private List<Message> list = new ArrayList<>();
    private DelMessageModel messageModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_messgae, "订单消息", MODE_BACK_NAVIGATION);
        ButterKnife.bind(this);
        messageDB = new MessageDB(mContext);
        messageDB.updateMessageRead("2");
        list = messageDB.getByTypeMessages("2");
        mSwipeMenuRecyclerView.setLayoutManager(new LinearLayoutManager(this));// 布局管理器。
        mSwipeMenuRecyclerView.setHasFixedSize(true);// 如果Item够简单，高度是确定的，打开FixSize将提高性能。
        mSwipeMenuRecyclerView.setItemAnimator(new DefaultItemAnimator());// 设置Item默认动画，加也行，不加也行。
        mSwipeMenuRecyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.HORIZONTAL, Utils.dip2px(mContext, 0.5F), getResources().getColor(R.color.line_color)));
        // 为SwipeRecyclerView的Item创建菜单就两句话，不错就是这么简单：
        // 设置菜单创建器。
        mSwipeMenuRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        // 设置菜单Item点击监听。
        mSwipeMenuRecyclerView.setSwipeMenuItemClickListener(menuItemClickListener);
        if (list.size() == 0) {
            iv_nodata.setVisibility(View.VISIBLE);
        } else {
            iv_nodata.setVisibility(View.GONE);
        }
        adapter = new MenuOrderAdapter(list);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Message message = list.get(position);
                Bundle bundle = new Bundle();
                bundle.putString("order_id", message.getData_id());
                startAct(OrderDetailsActivity.class, bundle);
            }
        });
        mSwipeMenuRecyclerView.setAdapter(adapter);
    }


    /**
     * 菜单点击监听。
     */
    private OnSwipeMenuItemClickListener menuItemClickListener = new OnSwipeMenuItemClickListener() {
        /**
         * Item的菜单被点击的时候调用。
         * @param closeable       closeable. 用来关闭菜单。
         * @param adapterPosition adapterPosition. 这个菜单所在的item在Adapter中position。
         * @param menuPosition    menuPosition. 这个菜单的position。比如你为某个Item创建了2个MenuItem，那么这个position可能是是 0、1，
         * @param direction       如果是左侧菜单，值是：SwipeMenuRecyclerView#LEFT_DIRECTION，如果是右侧菜单，值是：SwipeMenuRecyclerView#RIGHT_DIRECTION.
         */
        @Override
        public void onItemClick(Closeable closeable, int adapterPosition, int menuPosition, int direction) {
            closeable.smoothCloseMenu();// 关闭被点击的菜单。
            // TODO 如果是删除：推荐调用Adapter.notifyItemRemoved(position)，不推荐Adapter.notifyDataSetChanged();
            if (menuPosition == 0) {// 删除按钮被点击。
                Message message = list.get(adapterPosition);
                messageModel = new DelMessagePresenter(OrderMessageActivity.this, mContext);
                messageModel.deleteMessage(message.getMid() + "");
                messageDB.deleteMessage(message.getMid() + "");
                adapter.notifyItemRemoved(adapterPosition);
            }
        }
    };

    /**
     * 菜单创建器。在Item要创建菜单的时候调用。
     */
    private SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
        @Override
        public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
            int width = getResources().getDimensionPixelSize(R.dimen.item_height);

            // MATCH_PARENT 自适应高度，保持和内容一样高；也可以指定菜单具体高度，也可以用WRAP_CONTENT。
            int height = ViewGroup.LayoutParams.MATCH_PARENT;

            // 添加右侧的，如果不添加，则右侧不会出现菜单。
            {
                SwipeMenuItem deleteItem = new SwipeMenuItem(mContext)
                        .setBackgroundDrawable(R.drawable.selector_red)
                        .setImage(R.mipmap.ic_action_delete)
                        .setText("删除") // 文字，还可以设置文字颜色，大小等。。
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。
            }
        }
    };


    @Override
    public void deleteMessageSuccess(DelMessage message) {
        if (message != null) {
            if (message.getStatus() == HttpUtil.RESULT_OK) {
                list = messageDB.getByTypeMessages("2");// 系统消息
                if (list.size() == 0) {
                    iv_nodata.setVisibility(View.VISIBLE);
                } else {
                    iv_nodata.setVisibility(View.GONE);
                }
                adapter.setMessages(list);
                adapter.notifyDataSetChanged();
            } else {
                T.showToast(mContext, message.getMsg());
            }
        }
    }
}
